<!DOCTYPE html>
<html lang="cn" window-icon="./images/icon/icon.ico">
<head>
    <meta charset="UTF-8">
    <title>BS Redis Desktop Client</title>
</head>
<body>
<script type="module">
    import tools from "./js/tools"

    tools.lang("main.connectList.addBtn")
    const view = tools.getView()

    const init_sqlite = () => {
        let dir = tools.userAppDataDir() + "/BS Redis Desktop Client"
        // 判断文件夹是否存在
        let exist = tools.isPathExist(dir)
        if (!exist) {
            tools.sys.fs.$mkdir(dir)
        }
        dir = dir + "/data.db"
        setTimeout(() => {
            let res = view.xcall("init_sqlite", dir)
            if (res.status !== 1) {
                tools.alert("本地缓存初始化失败！软件无法使用！")
                view.xcall("exit")
                return
            }
            tools.dispatchGlobalEvt("sqlite-init")
        })
    }
    init_sqlite()
    const init = async () => {
        tools.getView().trayIcon({
            image: await Graphics.Image.load(__DIR__ + "images/icon/tray-icon.png"),
            text: "BS Redis Desktop Client"
        })
    }
    init()

    let menu = null
    view.on("trayiconclick", evt => {
        if (evt.data.buttons === 2) {
            if (menu) {
                return
            }
            menu = new Window({
                type: Window.POPUP_WINDOW,
                x: evt.data.screenX - 3,
                y: evt.data.screenY - 141,
                width: 120,
                height: 144,
                url: "menu.html"
            })
            menu.on("activate", evt => {
                if (evt.reason === 0) {
                    menu.close("close")
                }
            })
            menu.on("close", () => {
                menu = null
            })
            menu.on("exit", () => {
                tools.getView().trayIcon("remove")
                view.xcall("exit")
            })
            menu.on("show-main", () => {
                view.xcall("global_event", "show-main", true)
            })
        } else {
            view.xcall("global_event", "show-main", true)
        }
    })
    tools.bindGlobalEvt("clear-trayIcon", () => {
        tools.getView().trayIcon("remove")
        view.xcall("exit")
    })
    view.xcall("create_main_window")
</script>
</body>
</html>